<?php

class ReviewsController extends Controller {

    public function actionIndex() {
        $this->pageTitle = 'Отзывы — Моби03';
        Yii::app()->clientScript->registerMetaTag('', 'description');
        Yii::app()->clientScript->registerMetaTag('Отзывы', 'keywords');
        $limit = 5;
        $criteria = new CDbCriteria();
        $criteria->limit = $limit;
        $audioReviews = AudioReviews::model()->findAll($criteria);
        $totalCount = AudioReviews::model()->count();

        $criteria = new CDbCriteria();
        $criteria->compare('"reviewActive"', 1);
        $criteria->compare('"reviewRating"', ' > ' . 2);
        $criteria->compare('"reviewParent"', 0);
//        if (isset(Yii::app()->session['userReviews']) && !empty(Yii::app()->session['userReviews']))
//            $criteria->addCondition('"reviewId" IN(' . Yii::app()->session['userReviews'] . ') AND "reviewParent" = 0', 'OR');
        $criteria->order = '"reviewDate" desc';
        $count = Reviews::model()->count($criteria);
        $pages = new CPagination($count);
        $pages->pageSize = 10;
        $pages->applyLimit($criteria);
        $models = Reviews::model()->findAll($criteria);

        $model = new MbReviews();

        if (isset($_POST['MbReviews'])) {
            if (isset($_GET['parent']))
                $model->reviewParent = (int) $_GET['parent'];
            $model->attributes = $_POST['MbReviews'];
            $model->reviewActive = 0;
            $model->reviewDate = time();

            if ($model->save()) {
                $content = 'Имя: ' . $model->reviewUsername . '<br />Телефон: ' . $model->reviewUserphone . '<br /> Номер заказа: ' . $model->reviewOrderNo . '<br />' . $model->reviewContent;
                mTools::sendMail('clients@mobi03.ru', 'Жалоба клиента с сайта mobi03.ru', $content);
                if (isset(Yii::app()->session['userReviews']) && !empty(Yii::app()->session['userReviews'])) {
                    Yii::app()->session['userReviews'] = Yii::app()->session['userReviews'] . ',' . $model->reviewId;
                } else {
                    Yii::app()->session['userReviews'] = $model->reviewId;
                }
                Yii::app()->user->setFlash('success', ['Ваш отзыв отправлен', '<p>Благодарим!</p> <p> Ваш отзыв успешно отправлен.</p> <p>Он появится в ветке отзывов товара как только наши модераторы рассмотрят его.</p>']);
                $this->redirect('/otzivi');
            }
        } else {
            $model->reviewType = 1;
        }

        $this->render('index', array('audioReviews' => $audioReviews, 'totalCount' => $totalCount, 'limit' => $limit, 'models' => $models, 'pages' => $pages, 'model' => $model));
    }

    public function actionError() {
        $this->render('error');
    }

    function recursive($parent, $dataParent = 0) {
        $userReview = '';
//        if (isset(Yii::app()->session['userReviews']) && !empty(Yii::app()->session['userReviews']))
//            $userReview = 'OR "reviewId" IN(' . Yii::app()->session['userReviews'] . ')';
        $sql = "SELECT \"reviewId\", \"reviewContent\", \"reviewUsername\", \"reviewType\",\"reviewDate\" FROM {{reviews}} WHERE \"reviewParent\" = '$parent' AND (\"reviewActive\" = 1 $userReview)";
        $array = Yii::app()->db->getCommandBuilder()->createSqlCommand($sql)->queryAll();
        echo '<ul>';
        foreach ($array as $item) {
            echo '<li class="child">';
            echo '<h3>' . $item['reviewUsername'] . '</h3><span class="span-date__right">' . date("d.m.Y г.", $item['reviewDate']) . '</span><p>' . $item['reviewContent'] . '</p>';
//            echo '<span class="feedback-list__form__link" data-parent="' . $dataParent . '" data-url="/otzivi?parent=' . $item['reviewId'] . '">Ответить</span>';
//            echo '<br /><br />';

            echo '</li>';
            $this->recursive($item['reviewId'], $dataParent);
        }
        echo '</ul>';
    }

}
